using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 击飞Buff - 将目标击飞到空中
/// </summary>
public class KnockUpBuff : BuffBase
{
    private Vector3 m_KnockUpDirection;
    private float m_KnockUpForce;
    private float m_KnockUpHeight;
    private bool m_IsKnockedUp;
    private float m_OriginalGravity;

    public KnockUpBuff(BuffData buffData, Entity caster, Entity target) : base(buffData, caster, target)
    {
        if (buffData is KnockUpData knockUpData)
        {
            m_KnockUpForce = knockUpData.KnockUpForce;
            m_KnockUpHeight = knockUpData.KnockUpHeight;
        }
        else
        {
            m_KnockUpForce = 10.0f; // 默认击飞力度
            m_KnockUpHeight = 3.0f; // 默认击飞高度
        }
        
        // 计算击飞方向（向上）
        m_KnockUpDirection = Vector3.up;
    }

    protected override void OnStart()
    {
        base.OnStart();
        
        m_IsKnockedUp = true;
        
        // 禁用移动组件
        if (Target.MoveComponent != null)
        {
            Target.MoveComponent.enabled = false;
        }
        
        // 播放击飞特效
        if (BuffData.Effect != null)
        {
            var effect = GameObject.Instantiate(BuffData.Effect, Target.transform);
            effect.transform.localPosition = Vector3.zero;
        }
        
        // 设置动画状���
        if (Target.Animator != null)
        {
            Target.Animator.SetBool("knockedUp", true);
        }
        
        Logger.Log($"{Target.name} 被击飞到空中");
    }

    protected override void OnTick(float dt)
    {
        base.OnTick(dt);
        
        if (m_IsKnockedUp)
        {
            // 执行击飞移动
            Vector3 currentPos = Target.transform.position;
            Vector3 newPos = currentPos + m_KnockUpDirection * m_KnockUpForce * dt;
            
            // 检查是否达到击飞高���
            if (newPos.y >= m_KnockUpHeight)
            {
                // 开始下���
                m_KnockUpDirection = Vector3.down;
                m_KnockUpForce *= 0.5f; // 降低下落速度
            }
            
            // 检查是否落���
            if (newPos.y <= 0)
            {
                newPos.y = 0;
                m_IsKnockedUp = false;
            }
            
            Target.transform.position = newPos;
        }
    }

    protected override void OnFinish()
    {
        base.OnFinish();
        m_IsKnockedUp = false;
        Logger.Log($"{Target.name} 击飞效果结束");
    }

    protected override void OnRemove()
    {
        base.OnRemove();
        
        m_IsKnockedUp = false;
        
        // 恢复移动组件
        if (Target.MoveComponent != null)
        {
            Target.MoveComponent.enabled = true;
        }
        
        // 恢复动画状���
        if (Target.Animator != null)
        {
            Target.Animator.SetBool("knockedUp", false);
        }
        
        // 清理特效
        var effects = Target.GetComponentsInChildren<ParticleSystem>();
        foreach (var effect in effects)
        {
            if (effect.name.Contains("KnockUp"))
            {
                GameObject.Destroy(effect.gameObject);
            }
        }
    }

    public override bool IsCanMerge()
    {
        return false; // 击飞效果不能叠加，只能刷新持续时���
    }
}
